/*
 * Copyright 2012 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudgettable.client.ui.grid.cell.date;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.datepicker.client.CalendarUtil;
import finbudgettable.client.ui.grid.DateUtils;

import java.util.Date;

public class DateCellUtil {
    public static String resolve(Date beginDate, Date endDate, DateTimeFormat format) {
        int beginDay = DateUtils.getMonthDay(beginDate);
        int endDay = DateUtils.getMonthDay(endDate);
        boolean standardPeriod = beginDay == 1 && endDay == 1;
        if (standardPeriod) {
            Date nextYear = DateUtils.addYears(beginDate, 1);
            if (nextYear.equals(endDate)) {
                int thisYear = DateUtils.getYear(new Date());
                if (thisYear == DateUtils.getYear(endDate)) {
                    return DateContextMenu.CONSTANTS.lastYr();
                }
                if (thisYear + 1 == DateUtils.getYear(endDate)) {
                    return DateContextMenu.CONSTANTS.thisYr();
                }
                return DateContextMenu.CONSTANTS.inYr(DateUtils.getYear(beginDate));
            } else {
                int beginMonth = DateUtils.getMonth(beginDate);
                int endMonth = DateUtils.getMonth(endDate);
                int monthCount = endMonth - beginMonth;
                if (monthCount == 3 || monthCount == -9) {
                    int thisQrMonth = (DateUtils.getMonth(new Date()) / 3) * 3;
                    if (beginMonth == thisQrMonth) {
                        return DateContextMenu.CONSTANTS.thisQr();
                    }
                    int prevQrMonth = (12 + thisQrMonth - 3) % 12;
                    if (beginMonth == prevQrMonth) {
                        return DateContextMenu.CONSTANTS.lastQr();
                    }
                    int beginYear = DateUtils.getYear(beginDate);
                    String beginQr = DateContextMenu.QUARTERS[beginMonth / 3];
                    return DateContextMenu.CONSTANTS.inQrYr(beginQr, beginYear);
                } else if (monthCount == 1 || monthCount == -11) {
                    int beginYear = DateUtils.getYear(beginDate);
                    return DateContextMenu.CONSTANTS.inMnYr(Integer.toString(beginMonth + 1), beginYear);
                }
            }
        }
        Date nextDate = CalendarUtil.copyDate(beginDate);
        CalendarUtil.addDaysToDate(nextDate, 1);
        if (endDate.equals(nextDate)) {
            return DateContextMenu.CONSTANTS.date(DateCell.FORMAT.format(beginDate));
        }
        return "c " + format.format(beginDate) + " до " + DateCell.FORMAT.format(endDate);
    }
}
